package com.sigmazero13.gameutils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.sigmazero13.gameutils.model.GamePlayInfo;
import com.sigmazero13.gameutils.utils.PlayInfoPlayerAdapter;

public class PlayInfoView extends Activity {

	private long playId;
	private ListView playerList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.play_info);
		
		Bundle bundle = getIntent().getExtras();
		playId = bundle.getLong("play_id");
		
		playerList = (ListView)findViewById(R.id.play_info_player_list);
		
		update();
		this.setResult(RESULT_CANCELED);
	}
	
	private void update() {
		GamePlayInfo play = ((BoardGameUtilsApp)getApplication()).getDbHelper().selectSinglePlay(playId);
		
		String gameName = play.getGame().getName();
		((TextView)findViewById(R.id.game_name)).setText(gameName);
		
		String gameDate = play.getGameDate() != null ? DateFormat.getDateFormat(this).format(play.getGameDate()) : null;
		updateField("date", gameDate);
		updateField("location", play.getLocation());
		updateField("scenario", play.getScenario());
		
		String gameLength = null;
		if (play.getGameLength() != null && play.getGameLength() > 0) {
			gameLength = play.getGameLength().toString() + " minutes";
		}
		updateField("gamelength", gameLength);
		
		updateField("numplayers", play.getNumPlayers().toString());
		
		PlayInfoPlayerAdapter adapter = new PlayInfoPlayerAdapter(this, R.layout.play_info_player, play.getPlayers());
		playerList.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
	
	private void updateField(String fieldNameRoot, String fieldValue) {
		if (fieldValue == null || fieldValue.equals("")) {
			Integer sectionId = getResources().getIdentifier(fieldNameRoot+"_section", "id", this.getPackageName());
			findViewById(sectionId).setVisibility(View.GONE);
		} else {
			Integer valueId = getResources().getIdentifier(fieldNameRoot+"_value", "id", this.getPackageName());
			((TextView)findViewById(valueId)).setText(fieldValue);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.play_info_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch(item.getItemId()) {
		case R.id.edit_play:
			i = new Intent(PlayInfoView.this, RecordGameConfig.class);
			i.putExtra("play_id", playId);
			startActivityForResult(i, 0);
		}
		
		return true;
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK) {
        	Intent i = new Intent();
        	i.putExtra("play_id", playId);
        	this.setResult(RESULT_OK, i);
        }
        update();
	}
}
